package com.example.jpatest.aspect;

import com.example.jpatest.exception.REnum;
import com.example.jpatest.exception.RException;
import com.example.jpatest.utils.RUtil;
import com.example.jpatest.vo.R;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 统一异常处理
 */
@RestControllerAdvice
public class ExceptionHandel {
    @ExceptionHandler(value=Exception.class)

    public R handel(Exception e){
        if(e instanceof RException){
            RException rException=(RException) e;
            return RUtil.error(rException.getCode(),rException.getMessage());
        }
        return RUtil.error(REnum.UNKOWN_ERR);
    }
}
